package com.jiang.myfm.adapters;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.jiang.myfm.R;
import com.jiang.myfm.entities.guobobean.Title;

import java.util.List;

/**
 * Created by Administrator on 2016/12/7.
 */
public class Find3adaptertit extends BaseAdapter {
    private Context context;
    private List<Title> list,otherlist;

    public Find3adaptertit(Context context, List<Title> list, List<Title> otherlist) {
        this.context = context;
        this.list = list;
        this.otherlist=otherlist;
    }

    @Override
    public int getCount() {
        return list.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==getCount()-1){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder holder;
        if(convertView==null){
            if(getItemViewType(position)==0) {
                convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
                holder = new Viewholder();
                holder.tv = (TextView) convertView;
                convertView.setTag(holder);
            }else{
                convertView=LayoutInflater.from(context).inflate(R.layout.button,parent,false);
                holder=new Viewholder();
                convertView.setTag(holder);
                CheckBox view= (CheckBox) convertView;
                view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            list.addAll(otherlist);
                            buttonView.setChecked(true);
                            notifyDataSetChanged();
                        }else {
                            list.removeAll(otherlist);
                            buttonView.setChecked(false);
                            notifyDataSetChanged();

                        }
                    }

            });}
        }else {
            holder= (Viewholder) convertView.getTag();
        }
        convertView.setForegroundGravity(Gravity.CENTER);
        if(getItemViewType(position)==0) {
            holder.tv.setText(list.get(position).getName());
        }

        return convertView;
    }
  public class Viewholder{
       private TextView tv;
   }
}
